package element.synt;

import element.base.Synt;
import element.base.TreeNode;
import symbal.Symbal;
import symbal.SymbalTable;
import table.InterCodeTable;

import java.util.ArrayList;

public class Block extends Synt {

    public void goDown() {
        InterCodeTable.symbalTable = new SymbalTable(InterCodeTable.symbalTable);
        InterCodeTable.constTable = new SymbalTable(InterCodeTable.constTable);
    }

    public void goUp() {
        InterCodeTable.symbalTable = InterCodeTable.symbalTable.getFather();
        InterCodeTable.constTable = InterCodeTable.constTable.getFather();
    }
    @Override
    public void tointerRP() {
        goDown();
        if (!InterCodeTable.paraTable.isEmpty()) {
            for (Symbal para: InterCodeTable.paraTable) {
                InterCodeTable.symbalTable.addSymbal(para);
            }
            InterCodeTable.paraTable.clear();
        }
        ArrayList<TreeNode> nodes = this.getNodes();
        for (TreeNode node: nodes) {
            if (node instanceof BlockItem) {
                ((BlockItem) node).tointerRP();
            }
        }
        goUp();
    }
}
